define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kpxt/kpinfo/index',
                    add_url: 'kpxt/kpinfo/add',
                    edit_url: 'kpxt/kpinfo/edit',
                    del_url: 'kpxt/kpinfo/del',
                    multi_url: 'kpxt/kpinfo/multi',
                    table: 'kpxt_kpinfo',
                }
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                $("#money").text(data.extend.money);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                search: false,
                sortName: 'id',
                searchFormVisible: true,
                dblClickToEdit: false, //是否启用双击编辑
                showExport: false,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        {field: 'dzkh', title: __('Dzkh'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'state', title: __('State'), searchList: {"待开":__('待开'),"已开":__('已开')}, formatter: Controller.api.formatter.state},
                        {field: 'sqriqi', title: __('Sqriqi'), addclass:'datetimepicker'},
                        {field: 'kpny', title: __('开票年月')},
                        {field: 'gsname', title: __('Gsname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'gssh', title: __('Gssh'), operate:false},
                        {field: 'hwhyslwfwmc', title: __('Hwhyslwfwmc'), operate:false},
                        {field: 'unit', title: __('Unit'), operate:false},
                        {field: 'num', title: __('Num'), operate:false},
                        {field: 'hsprice', title: __('Hsprice'), operate:false},
                        {field: 'sl', title: __('Sl'), operate:false},
                        {field: 'zprice', title: __('Zprice'), operate:false},
                        {field: 'pzlx', title: __('Pzlx'), searchList: {"增值税专票":__('增值税专票'),"增值税普票":__('增值税普票')}, formatter: Table.api.formatter.normal},
                        {field: 'khhzh', title: __('Khhzh'), operate:false},
                        {field: 'addr', title: __('Addr'), operate:false},
                        {field: 'tel', title: __('Tel'), operate:false},
                        {
                            field: 'fpbh',
                            title: __('Fpbh'),
                            editable: {
                                emptytext: '无',
                                rows:2,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false,
                        },
                        {
                            field: 'remarks',
                            title: __('Remarks'),
                            editable: {
                                emptytext: '无',
                                rows:2,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false,
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            var form = $("form.form-commonsearch");
            if ($(".datetimepicker", form).size() > 0) {
                require(['bootstrap-datetimepicker'], function () {
                    var options = {
                        format: 'YYYY-MM-DD',
                        icons: {
                            time: 'fa fa-clock-o',
                            date: 'fa fa-calendar',
                            up: 'fa fa-chevron-up',
                            down: 'fa fa-chevron-down',
                            previous: 'fa fa-chevron-left',
                            next: 'fa fa-chevron-right',
                            today: 'fa fa-history',
                            clear: 'fa fa-trash',
                            close: 'fa fa-remove'
                        },
                        showTodayButton: true,
                        showClose: true
                    };
                    $('.datetimepicker', form).parent().css('position', 'relative');
                    $('.datetimepicker', form).datetimepicker(options);
                });
            }


            //kpxt/kpinfo/index
            //点击状态
            $(document).on("dblclick", ".state[data-id]", function () {
                var url = 'kpxt/kpinfo/changestate';
                var id = $(this).data('id');
                var _this = $(this);
                Fast.api.ajax({
                    url: url,
                    data: {ids: id},
                }, function (data, ret) {
                    _this.html(ret.data.state);
                    if (ret.data.state != '已开') {
                        _this.addClass('text-danger');
                    } else {
                        _this.removeClass('text-danger')
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            //$(document).on("change", "#c-num", function () {
            //    updatezj()
            //});
            //
            //$(document).on("change", "#c-hsprice", function () {
            //    updatezj()
            //});
            //
            //function updatezj() {
            //    var num = $("#c-num").val();
            //    var hsprice = $("#c-hsprice").val();
            //
            //    var zprice = num * hsprice;
            //    $("#c-zprice").val(zprice);
            //}

            Controller.api.bindevent();
        },
        add2: function () {
            //$(document).on("change", "#c-num", function () {
            //    updatezj()
            //});
            //
            //$(document).on("change", "#c-hsprice", function () {
            //    updatezj()
            //});
            //
            //function updatezj() {
            //    var num = $("#c-num").val();
            //    var hsprice = $("#c-hsprice").val();
            //
            //    var zprice = num * hsprice;
            //    $("#c-zprice").val(zprice);
            //}


            Controller.api.bindevent();
        },
        edit: function () {
            //$(document).on("change", "#c-num", function () {
            //    updatezj()
            //});
            //
            //$(document).on("change", "#c-hsprice", function () {
            //    updatezj()
            //});
            //
            //function updatezj() {
            //    var num = $("#c-num").val();
            //    var hsprice = $("#c-hsprice").val();
            //
            //    var zprice = num * hsprice;
            //    $("#c-zprice").val(zprice);
            //}
            
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                state: function (value, row, index) {
                    if (row.state != '已开') {
                        return [
                            '<a href="javascript:;" class="state text-danger" data-id= "' + row.id + '" > ' + row.state + '</a>'
                        ].join("")

                    } else {
                        return [
                            '<a href="javascript:;" class="state" data-id= "' + row.id + '" > ' + row.state + '</a>'
                        ].join("")
                    }
                },
            }
        }
    };
    return Controller;
});